// pages/cart/cart.js
var cartAllUrl = require('../../config').cartAllUrl;
var aduCartUrl = require('../../config').aduCartUrl;


var app = getApp();

Page({

  data: {
    commNum:1,
    //  
    selectAll:false,
    total:0
  },
  onLoad: function (options) {
    var that = this;
    wx.request({
      url: cartAllUrl,
      method: 'get',
      data: {
        skey:wx.getStorageSync("skey")
        },
      success: function (res) {
        var cartList = res.data.cartList;
        console.log(cartList);
        for (var i = 0; i < cartList.length; i++) {
          var imgList = cartList[i].img;
          for (var j = 0; j < imgList.length; j++) {
            if (imgList[j].imgType === 1) {
              cartList[i].cover = imgList[j].imgUrl;
              cartList[i].select = false;
            }
          }
        }
        that.setData({
          cartList:res.data.cartList
        })
      }
    });
  },
  // 修改购物车数量
  operNumServe: function (num,cartId){
    wx.request({
      url: aduCartUrl,
      method: 'put',
      data: {
        skey: wx.getStorageSync("skey"),
        dishNum: num,
        cartId: cartId
      },
      success: function (res) {
      }
    })
  },
  /**
     *加减 
     */
  operNum: function (e) {
    var that = this;
    var oper = e.currentTarget.dataset.oper;
    var index = e.currentTarget.dataset.index;
    var data = that.data.cartList;
    var dishNum = data[index].cart.dishNum;
    var num;
    if (oper == '-') {
      if (dishNum == 1){
        num = 1
      }else{
        num = dishNum - 1
        if (data[index].select == true) {
          that.data.total = that.data.total - 1 * data[index].dish.price
        } 
        that.operNumServe(num, data[index].cart.id);
      }  
    } else {
      num = dishNum + 1;
      if (data[index].select == true) {
        that.data.total = that.data.total + 1 * data[index].dish.price
      }
      wx.request({
        url: aduCartUrl,
        method: 'put',
        data: {
          skey: wx.getStorageSync("skey"),
          dishNum: num,
          cartId: data[index].cart.id
        },
        success: function (res) {
        }
      })
    }
                   
    that.data.cartList[index].cart.dishNum = num
    that.setData({
      cartList: that.data.cartList,
      total: that.data.total
    })
   
  },
  // 
  selectShop:function(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    var allData = that.data.cartList;
    var selectLen = 0;
    var total = 0;
    that.data.cartList[index].select = !that.data.cartList[index].select;
    for (var i = 0; i < allData.length; i++) {
      if (allData[i].select == true){
        selectLen++;
        total += allData[i].cart.dishNum * allData[i].dish.price
      }
    }
    if (selectLen == allData.length){
      that.data.selectAll = true;
    }else{
      that.data.selectAll = false;
    }
    that.setData({
      cartList: that.data.cartList,
      selectAll: that.data.selectAll,
      total: total
    })
    
  },
  //全选
  selectAll:function(){
    var that = this;
    var data = that.data.cartList;
    var total = 0;
    for (var i = 0; i < data.length;i++){
      if (that.data.selectAll == false){
        that.data.cartList[i].select = true;
        total += data[i].cart.dishNum * data[i].dish.price
      }else{
        that.data.cartList[i].select = false;
        total = 0
      }
      
    }
    that.setData({
      cartList: that.data.cartList,
      selectAll : !that.data.selectAll,
      total: total
    })
  },
  // 删除购物车的商品
  delCart:function(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    var cartList = that.data.cartList;
    var id=cartList[index].cart.id;
    app.GBshowModalTip('提示', true, '是否删除该商品', confirmDel);
    var confirmDel = function(res){
      if (res.confirm){
        wx.request({
          url: aduCartUrl,
          method: 'delete',
          data: {
            skey: wx.getStorageSync("skey"),
            cartId: id
          },
          success: function (res) {
            if (res.data.code === 'ok') {
              cartList.splice(index, 1);
              that.setData({
                cartList: cartList
              })
            }
          }
        })
      }
    };
    

  },
  // 结算
  prepay:function(){
    var that = this;
    var payList = {};
    var cartList = that.data.cartList;
    var shopList=[];
    var totalFee = that.data.total;
    for(var i=0;i<cartList.length;i++){
      if(cartList[i].select===true){
        shopList.push(cartList[i])
      }
    }
    payList = {
      shopList: shopList,
      total: totalFee
    }
    wx.setStorageSync("payList", payList);
    wx.navigateTo({
      url: '../payment/payment?type=0'
    })
  }
})